package com.mashibing.service;

import com.mashibing.remote.DicDistrictClient;
import com.mashibing.internalcommon.constant.AmapConfigConstants;
import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.dto.DicDistrict;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.mapper.DicDistrictMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DicDistrictService {

    @Autowired
    private DicDistrictClient dicDistrictClient;

    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    /**
     * 生成中国行政区关系数据库表
     *
     * @param keyWord
     * @return
     */
    public ResponseResult generateDicDistrict(String keyWord) {

        // 调用高德接口
        String result = dicDistrictClient.getAllDistrict(keyWord);

        // 处理参数
        JSONObject resultJsonObject = JSONObject.fromObject(result);
        int status = resultJsonObject.getInt(AmapConfigConstants.STATUS);
        if (status != 1) {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(), CommonStatusEnum.MAP_DISTRICT_ERROR.getMessage(),null);
        }

        JSONArray resultJsonArray = resultJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
        analysisResultJsonObject(resultJsonArray, "");

        return ResponseResult.success();
    }


    /**
     * 解析高德返回结果
     *
     * @param resultJsonArray  高德结果集
     * @param parentAddressCode 父级地区编码
     * @return
     */
    public void analysisResultJsonObject(JSONArray resultJsonArray, String parentAddressCode) {
        if (!resultJsonArray.isEmpty()) {
            for (Object result : resultJsonArray) {
                DicDistrict dicDistrict = new DicDistrict();
                JSONObject resultJsonObj = JSONObject.fromObject(result);

                // 获取级别
                String level = resultJsonObj.get(AmapConfigConstants.LEVEL).toString();
                if (level.equals(AmapConfigConstants.STREET)) {
                    continue;
                }
                dicDistrict.setLevel(generateLevel(level));

                // 地区编码转换
                String adCode = resultJsonObj.get(AmapConfigConstants.ADCODE).toString();
                dicDistrict.setAddressCode(adCode);

                // 地区名称转换
                dicDistrict.setAddressName(resultJsonObj.get(AmapConfigConstants.NAME).toString());

                // 获取父级编码
                dicDistrict.setParentAddressCode(parentAddressCode);

                // 插入数据库
                dicDistrictMapper.insert(dicDistrict);

                // 递归
                analysisResultJsonObject(resultJsonObj.getJSONArray(AmapConfigConstants.DISTRICTS), adCode);
            }
        }
    }

    public int generateLevel(String level) {
        int levelInt = 0;
        if (level.trim().equals("country")) {
            levelInt = 0;
        } else if (level.trim().equals("province")) {
            levelInt = 1;
        } else if (level.trim().equals("city")) {
            levelInt = 2;
        } else if (level.trim().equals("district")) {
            levelInt = 3;
        }
        return levelInt;
    }


}
